package com.kuaimacode.kframework.agent.util;

import com.kuaimacode.kframework.common.Constant;
import org.apache.shiro.SecurityUtils;
import org.springframework.util.Assert;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.util
 * @Description: created by IntelliJ IDEA
 * @date 2019-04-05 21:33
 */
public class SessionUtil {

    /**
     * 存放值
     * @param key
     * @param value
     * @param <V>
     */
    public static <V> void put(String key, V value) {
        SecurityUtils.getSubject().getSession().setAttribute(key, value);
    }

    /**
     * 获取值
     * @param key
     * @param <V>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <V> V get(String key) {
        return (V) SecurityUtils.getSubject().getSession().getAttribute(key);
    }

    /**
     * 获取登陆用户ID
     * @return
     */
    public static Long getUserId() {
        return get(Constant.AGENT_USER_ID);
    }

    public static void putUserId(Long userId) {
        Assert.notNull(userId, "userId不能为空");
        put(Constant.AGENT_USER_ID, userId);
    }

    public static void putAgentId(String agentId) {
        Assert.hasText(agentId, "agentId不能为空");
        put(Constant.AGENT_ID, agentId);
    }

    /**
     * 获取分站ID
     * @return
     */
    public static String getAgentId() {
        return get(Constant.AGENT_ID);
    }
}
